package com.zyg.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.page.entity.Student;
import com.zyg.page.entity.vo.StudentVo;
import com.zyg.mapper.ClassesMapper;
import com.zyg.mapper.StudentMapper;
import com.zyg.user.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    //1、查询所有学生
    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectList(null);
        for (Student student : students) {
            student.setCname(classesMapper.selectById(student.getCid()).getCname());
        }
        return students;
    }

    //根据学号查询学生
    @Override
    public Student findById(int sid) {
        return studentMapper.selectById(sid);
    }

    //添加学生
    @Override
    public void add(Student student) {
       studentMapper.insert(student);
    }

    //修改学生
    @Override
    public void update(Student student) {
        studentMapper.updateById(student);
    }
    //删除学生
    @Override
    public void delete(int sid) {
        studentMapper.deleteById(sid);
    }

    //分页查询
    @Override
    public IPage<Student> findByPage(int page, int pageSize) {
        Page<Student> ipage = new Page<>(page,pageSize);
        Wrapper<Student> wrapper = new QueryWrapper<>();
       return studentMapper.selectPage(ipage,wrapper);
    }
    //条件分页查询
    @Override
    public IPage<Student> search(int page, int pageSize, StudentVo studentVo) {
        Page<Student> studentPage = new Page<>();
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        if (studentVo!=null){
            if (!StringUtils.isEmpty(studentVo.getSname())){
                studentQueryWrapper.like("sname",studentVo.getSname());
            }
            if (!StringUtils.isEmpty(studentVo.getAddr())){
                studentQueryWrapper.like("addr",studentVo.getAddr());
            }
            if (studentVo.getCid()!= null){
                studentQueryWrapper.like("cid",studentVo.getCid());
            }
        }
        return studentMapper.selectPage(studentPage,studentQueryWrapper);
    }
}
